import java.net.*;
import java.io.*;

public class TCPClient {
    
    public static void main(String args[]){
        // args[0] <- hostname of destination
	if (args.length == 0) {
	    System.out.println("java TCPClient hostname");
	    System.exit(0);
	}
        
        Socket clientSocket = null;
        int serverSocket=6000;
        
        try{
            clientSocket = new Socket (args[0], serverSocket);
            System.out.println("SOCKET=" + clientSocket);
            
	    DataInputStream in = new DataInputStream(clientSocket.getInputStream());
	    DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());

	    String texto = "";
	    InputStreamReader input = new InputStreamReader(System.in);
	    BufferedReader reader = new BufferedReader(input);
	    System.out.println("Introduza texto:");

	    while (true) {
		// READ STRING FROM KEYBOARD
		try {
		    texto = reader.readLine();
		} catch (Exception e) {}
		// WRITE INTO THE SOCKET
		out.writeUTF(texto);
		// READ FROM SOCKET
		String data = in.readUTF();
		// DISPLAY WHAT WAS READ
		System.out.println("Received: " + data);
	    }
            
        }catch (UnknownHostException e) {
           System.out.println("Sock:" + e.getMessage());
        } catch (EOFException e) {
           System.out.println("EOF:" + e.getMessage());
        } catch (IOException e) {
           System.out.println("IO:" + e.getMessage());
        }
    }
}
